package com.talent.service.impl;

import com.talent.common.Const;
import com.talent.dao.UserMapper;
import com.talent.page.Page;
import com.talent.pojo.User;
import com.talent.service.UserService;
import com.talent.utils.MD5Util;
import com.talent.vo.UserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public UserVo getUserVoById(Integer id) {
        return userMapper.selectUserVoById(id);
    }

    @Override
    public User login(String name, String password) {
        int resultCount = userMapper.checkName(name);
        if(resultCount == 0){
            return null;
        }
        String md5Password = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectLogin(name,md5Password);
        if(user == null){
            return null;
        }

        user.setPassword(StringUtils.EMPTY);
        return user;
    }


    @Override
    public Page<UserVo> getUserVoPage(UserVo userVo, Integer type, int currentPage) {
        Page<UserVo> page = new Page<>(currentPage, Page.DEFAULT_PAGE_SIZE);
        if(type == 0){
            userVo.setType(Const.USER_TYPE.USER_TYPE_ADMIN.getCode());
        }else if(type == 1){
            userVo.setType(Const.USER_TYPE.USER_TYPE_DEPART.getCode());
        }else{
            userVo.setType(Const.USER_TYPE.USER_TYPE_MAJOR.getCode());
        }
        List<UserVo> list = userMapper.selectUserVoPage(userVo, page);
        page.setResultList(list);
        return page;
    }

    @Override
    public void deleteAll(String[] ids) {
        userMapper.deleteAll(ids);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addUserVo(UserVo userVo) {
        userVo.setPassword(MD5Util.MD5EncodeUtf8(Const.DEFAULT_PASSWORD));
        userMapper.insertSelective(userVo);
    }

    @Override
    public int selectCount(String code) {
        return userMapper.selectCount(code);
    }

    @Override
    public UserVo selectById(Integer id) {
        return userMapper.selectUserVoById(id);
    }

    @Override
    public void editUserVo(UserVo userVo) {
        userMapper.updateByPrimaryKeySelective(userVo);
    }

    @Override
    public void editPassword(String oldPassword, String password, User user) {
        int resultCount = userMapper.checkPassword(MD5Util.MD5EncodeUtf8(oldPassword), user.getId());
        if(resultCount != 0){
            String md5Password = MD5Util.MD5EncodeUtf8(password);
            userMapper.editPassword(user.getId(), md5Password);
        }
    }

    @Override
    public int validate(String password, User user) {
        return userMapper.checkPassword(MD5Util.MD5EncodeUtf8(password), user.getId());
    }

    @Override
    public void resetPassword(Integer id) {
        String md5Password = MD5Util.MD5EncodeUtf8(Const.DEFAULT_PASSWORD);
        userMapper.editPassword(id, md5Password);
    }
}
